package action.upload;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import common.impl.exception.ServiceException;
import common.interf.IErrorHandle;
import common.interf.IFileHandle;
import common.interf.IImage;
import common.interf.IServletHandle;

public class ImageAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String filePath = "/upload/images";
	private IImage iImage;
	private IServletHandle servletHandle;
	private IFileHandle fileHandle;
	private IErrorHandle errorHandle;
	private File file;
	private File upfile;
	private File sharefile;
	private String sharefileFileName;
	private String upfileContentType;
	private String upfileFileName;
	private String fileContentType;
	private String fileFileName;
	private Integer size=0;
	private Integer width=0;
	private Integer height=0;
	private String callback;
	
	public File getSharefile() {
		return sharefile;
	}

	public void setSharefile(File sharefile) {
		this.sharefile = sharefile;
	}

	public String getSharefileFileName() {
		return sharefileFileName;
	}

	public void setSharefileFileName(String sharefileFileName) {
		this.sharefileFileName = sharefileFileName;
	}

	public void setiImage(IImage iImage) {
		this.iImage = iImage;
	}

	public void setServletHandle(IServletHandle servletHandle) {
		this.servletHandle = servletHandle;
	}

	public void setFileHandle(IFileHandle fileHandle) {
		this.fileHandle = fileHandle;
	}

	public void setErrorHandle(IErrorHandle errorHandle) {
		this.errorHandle = errorHandle;
	}

	public File getUpfile() {
		return upfile;
	}

	public void setUpfile(File upfile) {
		this.upfile = upfile;
	}

	public String getUpfileContentType() {
		return upfileContentType;
	}

	public void setUpfileContentType(String upfileContentType) {
		this.upfileContentType = upfileContentType;
	}

	public String getUpfileFileName() {
		return upfileFileName;
	}

	public void setUpfileFileName(String upfileFileName) {
		this.upfileFileName = upfileFileName;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	
	public void save(){
		String s="", result="";
		try{
			HttpServletRequest h = ServletActionContext.getRequest();
			Enumeration<String> aNames = h.getAttributeNames();
			Enumeration<String> pNames = h.getParameterNames();
			if(null != file){
				upfileFileName = fileFileName;
				upfile = file;
			}
			if(null != sharefile){
				upfileFileName = sharefileFileName;
				upfile = sharefile;
			}
			if(!iImage.checkImage(upfile, size, width, height)){
				throw new ServiceException("ERR-0020");
			}
			s = fileHandle.saveUploadFile(upfile, ServletActionContext.getServletContext().getRealPath(filePath), upfileFileName);
//			s = fileHandle.ToPng(ServletActionContext.getServletContext().getRealPath(filePath), s);
			String serverUrl = servletHandle.getServerPath(ServletActionContext.getRequest());
			s = serverUrl + filePath + "/" + s;
			result = "{\"name\":\""+ s +"\", \"originalName\": \""+ upfileFileName +"\", \"size\": 0, \"state\": \"SUCCESS\", \"type\": \"jpg\", \"url\": \""+ s +"\"}";
			result = result.replaceAll( "\\\\", "\\\\" );
			ServletActionContext.getResponse().getWriter().print(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getCallback() {
		return callback;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public IErrorHandle getErrorHandle() {
		return errorHandle;
	}
}
